package com.sniper.serviceribbon.service;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

/**
 * @author suzhen
 * @create 2018/3/27
 */
@Service
public class DbaUploadService {

    private static Logger logger = LoggerFactory.getLogger(DbaUploadService.class);

    @Autowired
    RestTemplate restTemplate;

    @HystrixCommand(fallbackMethod = "uploadError")
    public Map<String, Object> upload(MultipartFile multipartFile) {

        ParameterizedTypeReference<Map<String, Object>> responseType = new ParameterizedTypeReference<Map<String, Object>>() {
        };

        ResponseEntity<Map<String, Object>> responseEntity = restTemplate.exchange("http://SERVICE-DBA/upload", HttpMethod.GET, null, responseType, multipartFile);
        return responseEntity.getBody();
    }

    public Map<String, Object> uploadError(MultipartFile multipartFile) {

        return new HashMap<>(0);
    }
}
